﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Article : System.Web.UI.Page
{
    protected string sTitle = string.Empty;
    protected ArticleItem article;
    protected CategoryItem catItem;
    protected void Page_Load(object sender, EventArgs e)
    {
        GetParameters();
        
        if (article.articleId != 0)
        {
            HtmlMeta meta = new HtmlMeta();
            meta.Name = "keywords";
            meta.Content = PageHeaderData.GetArticleKeywords((int)article.articleId);
            this.Header.Controls.Add(meta);

            //Set title line
            catItem = Category.ItemById(article.catId);

            if (catItem.Ancestor != -1)
            {
                CategoryItem parent = Category.ItemById(catItem.Ancestor);
                if (parent.Ancestor != -1)
                {
                    string sLink = string.Format("<img src=\"images/bullet4.gif\" alt=\"bullet\"\" />" +
                            "<a href='{0}-w.aspx'>" +
                            "{1}</a><img src=\"images/bullet4.gif\" />" +
                            "<a href='{2}-w.aspx'>" +
                            "{3}</a>", parent.cPageName, parent.cTitle, catItem.cPageName, catItem.cTitle);
                    divTitleLink.InnerHtml += sLink;
                }
                else
                {
                    string sLink = string.Format("<img src=\"images/bullet4.gif\" alt=\"bullet\" />" +
                         "<a href='{0}-w.aspx'>" +
                         "{1}</a>", catItem.cPageName, catItem.cTitle);
                    divTitleLink.InnerHtml += sLink;
                }
                
            }

            sTitle = catItem.cTitle;
           
            ucHeader1.index = "5";

            // hide date
            if (!article.isDateVisible)
                divDate.Visible = false;
            // set tags
            rptTags.DataSource = article._tags;
            if (article._tags==null||article._tags.Rows.Count == 0)
            {
                divTags.Visible = false;
            }
            else
                rptTags.DataBind();

            Page.Title = article.BrowserTitle;
            
            // Get news by theme
            Dal.arParams.Clear();
            Dal.arParams.Add(catItem.catId);//CatID
            Dal.arParams.Add(article.articleId);//ArticleID
            DataTable DT = Dal.ExecuteDBsp("usp_GetNewsByCategory", Dal.arParams);
            if (DT != null)
                if (DT.Rows.Count > 0)
                {
                    lblMoreNews.Visible = true;
                    gvNews.DataSource = DT;
                    gvNews.DataBind();
                }
        }
    }

    private void GetParameters()
    {
        string sFilename = HttpUtility.UrlDecode(Request.Params["a"], System.Text.Encoding.UTF8);
       

        if (sFilename == "deadsea")
        {
            article = Articles.GetArticleCatID(10); // Lechenie na mertvom more
        }
        else
        {
            article = Articles.GetArticleByName(sFilename);
        }
        
    }

}
